import type { TColumn, TDataRow } from '@/components/editable-excel-table';
import type { DataIndex, TableAction } from './types';

export const actions = {
    setData<C extends TColumn>(data: TDataRow<C>[]): TableAction<C> {
        return { type: 'SET_DATA', payload: data } as TableAction<C>;
    },

    setActiveCell<C extends TColumn>(payload?: { rowIndex: number; dataIndex: DataIndex<C> }): TableAction<C> {
        return { type: 'SET_ACTIVE_CELL', payload } as TableAction<C>;
    },
    setEditingCell<C extends TColumn>(payload?: { rowIndex: number; dataIndex: DataIndex<C> }): TableAction<C> {
        return { type: 'SET_EDITING_CELL', payload } as TableAction<C>;
    },
    setColumnsWidth<C extends TColumn>(dataIndex: DataIndex<C>, width: number): TableAction<C> {
        return { type: 'SET_COLUMNS_WIDTH', payload: { dataIndex, width } } as TableAction<C>;
    },
    deleteRow<C extends TColumn>(rowIndex: number): TableAction<C> {
        return { type: 'DELETE_ROW', payload: rowIndex } as TableAction<C>;
    },
    updateCellValue<C extends TColumn>(rowIndex: number, dataIndex: DataIndex<C>, value: string): TableAction<C> {
        return { type: 'UPDATE_CELL_VALUE', payload: { rowIndex, dataIndex, value } } as TableAction<C>;
    },
    navigateToError<C extends TColumn>(direction: 'next' | 'prev'): TableAction<C> {
        return { type: 'NAVIGATE_TO_ERROR', payload: direction } as TableAction<C>;
    },
    setErrorPositions<C extends TColumn>(positions: Array<{ rowIndex: number; columnIndex: number }>): TableAction<C> {
        return { type: 'SET_ERROR_POSITIONS', payload: positions } as TableAction<C>;
    },
    setErrorCount<C extends TColumn>(totalErrors: number, fixedErrorCount: number): TableAction<C> {
        return { type: 'SET_ERROR_COUNT', payload: { totalErrors, fixedErrorCount } } as TableAction<C>;
    },
    transformState<C extends TColumn>(transformer: (state: any) => any): TableAction<C> {
        return { type: 'TRANSFORM_STATE', payload: transformer } as TableAction<C>;
    },
};
